package com.le.tester.compared;

import org.openjdk.jmh.annotations.*;
import org.openjdk.jmh.runner.Runner;
import org.openjdk.jmh.runner.RunnerException;
import org.openjdk.jmh.runner.options.Options;
import org.openjdk.jmh.runner.options.OptionsBuilder;

import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicLong;
import java.util.concurrent.atomic.LongAdder;

/**
 * createTime：2021/11/18 11:04
 * description：比较AtomicLong和LongAdder性能使用jmh
 */
//ops/time
@BenchmarkMode(Mode.Throughput)
@OutputTimeUnit(TimeUnit.MILLISECONDS)
public class PerformanceTest {
    private static AtomicLong atomicLong = new AtomicLong(0);

    //默认的初始值是0
    private static LongAdder longAdder = new LongAdder();


    @Benchmark
    //线程数
    @Threads(1)
    public void atomicLongIncrement() {
        atomicLong.getAndIncrement();
    }

    @Benchmark
    //线程数
    @Threads(1)
    public void longAdderIncrement() {
        longAdder.increment();
    }

    public static void main(String[] args) throws RunnerException {
        Options options = new OptionsBuilder().include(PerformanceTest.class.getSimpleName()).build();
        new Runner(options).run();
    }
}
